<?php
/**
 * Created by PhpStorm.
 * User: aDaddy
 * Date: 2016/7/25
 * Time: 9:55
 */

namespace Model\TeacherBundle\Model;
use Base\BaseModel;
use Model\TeacherBundle\Setting\TeacherApplySetting;

class TeacherApplyModel extends BaseModel{

    /*
     * 列表基本设置【通用模块必须开发】
     */
    public function listSettingDefault()
    {
        $settings = new TeacherApplySetting($this->getDoctrine());
        return $settings->listConfig();
    }

    /*
     * 为后台获取所有文章分类【通用模块必须开发】
     */
    /**
     * @param array $params $param[textKey]=textKey , $param[conditions][table][field][eq|gt|lt...]=value , $param[orderBy][table][field]=DESC|ASC
     * @param int $offset
     * @param int $perPage
     * @return array
     */
    public function getAdminLists($params ,$offset = 0 , $perPage = 10)
    {
        $qb = $this->getQueryFromParam($params);
        $lists = $this->getAllResult($qb,$offset,$perPage);
        $total = $this->countAllResult($qb);

        return array(
            "total" => $total ? $total : 0,
            "data"  => $lists
        );
    }

    public function addNewApply($params = array())
    {
        $defaults = array(
            "type"          => 0,//0=机构，1=个人
            "category"      => 0,//TeacherApplyCategory
            "name"          => "教师姓名未设置",
            "logo"          => "/",
            "intro"         => "个人简介未设置",
            "addTime"       => time(),
            "params"        => "[]",//Json 字符串
            "status"        => 0,//0=未审核，1=已审核
        );
        foreach ($defaults as $k => &$v) {
            if (isset($params[$k]) && $params[$k] !== "") {
                $v = $params[$k];
            }
        }

        $qb = $this->getQueryBuilder();
        $qb->insert("TeacherApply");

        $keys = array_keys($defaults);
        $values = array_values($defaults);
        $insertKeys = array();
        foreach ($keys as $k => $val) {
            $insertKeys[$val] = "?";
        }
        $qb->values($insertKeys)->setParameters($values);
        $rst = $qb->execute();
        return $qb->getState();
    }

    /**
     * @param $userId
     * @return mixed
     */
    public function getByUserId($userId)
    {
        $qb = $this->getQueryBuilder();
        $qb->select("*")->from("TeacherApply");
        $qb->where("userId = ?")->setParameter(0,$userId);
        return $qb->execute()->fetch();
    }
}